//  bigMall
//
//  Created by ios on 16/8/12.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGFuJiaCell.h"
#import "SZAddImage.h"

@interface BGFuJiaCell () {
    
}

@end

@implementation BGFuJiaCell {
    
    NSArray *dataArray;
    BOOL btnState;
    NSInteger Index;
    NSMutableArray *btnArr;
    UILabel *lab110;
    UITextField *filed110;
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    btnArr = [NSMutableArray array];
    dataArray = @[@"10000大卡",@"7000大卡",@"5000大卡",@"3000大卡",@"1000大卡"];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews {
    
    //设置标题的大小
    CGSize biaotiSize = [self.biaoti.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.biaoti.font,NSFontAttributeName, nil]];
    self.biaoti.width = biaotiSize.width;
    self.biaoti.height = biaotiSize.height;
    
    
}

- (void)jiazaishitu:(NSString *)stateType {
    
    if ([stateType isEqualToString:@"1"]) {
        
        //创建多选按钮啊
        int colCount = 3;
        CGFloat width = ([UIScreen mainScreen].bounds.size.width - 46 - 32) / 3;
        CGFloat heigh = 37;
        CGFloat maginX = 23;
        CGFloat maginY = 16;
        for (int i = 0 ; i < dataArray.count; i++) {
            int row = i / colCount;
            int col = i % colCount;
            CGFloat x = maginX + col * (width + 16);
            CGFloat y = maginY + 47 + row * (heigh + maginY);
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, heigh)];
            btn.tag = 10 + i;
            
            [btn setTitle:dataArray[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            // 按钮边框宽度
            btn.layer.borderWidth = 1.6;
            // 设置圆角
            btn.layer.cornerRadius = 10;
            // 设置颜色空间为rgb，用于生成ColorRef
            
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
            CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0, 250, 152, 1 });
            // 设置边框颜色
            btn.layer.borderColor = borderColorRef;
            
            [btnArr addObject:btn];
            
            [self.contentView addSubview:btn];
            
        }
        
    } else if ([stateType isEqualToString:@"2"]) {
        
        SZAddImage *a = [[SZAddImage alloc] initWithFrame:CGRectMake(27, 49, [UIScreen mainScreen].bounds.size.width - 27, [UIScreen mainScreen].bounds.size.height - 160)];
        
        [self.contentView addSubview:a];
        
    } else if ([stateType isEqualToString:@"3"]) {
     
        for (int i = 0; i < 3; i++) {
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(26, 49 + 41 * (i + 1), 16, 16)];
            btn.tag = i + 1;
            btn.tintColor = [UIColor blueColor];
            btn.backgroundColor = [UIColor blackColor];
            btn.clipsToBounds = YES;
            btn.layer.cornerRadius = 8;
            [btn addTarget:self action:@selector(clickChance:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:btn];
            
            lab110 = [[UILabel alloc] init];
            lab110.tag = i + 1;
            lab110.font = [UIFont systemFontOfSize:14];
            lab110.text = @"aaa";
            CGSize size = [lab110.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:lab110.font,NSFontAttributeName, nil]];
            lab110.frame = CGRectMake(btn.right + 10, 49 + (i + 1) * 41, size.width, size.height);
            [self.contentView addSubview:lab110];
            
            filed110 = [[UITextField alloc] initWithFrame:CGRectMake(lab110.right + 10, lab110.y, 100, lab110.height)];
            filed110.borderStyle = UITextBorderStyleNone;
            filed110.placeholder = @"填写浮动值";
            filed110.font = [UIFont systemFontOfSize:14];
            filed110.userInteractionEnabled = NO;
            filed110.tag = i + 1;
            [self.contentView addSubview:filed110];
            
        }
        
    } else {
        
        
        
    }
    
}

- (void)clickChance:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setBackgroundColor:[UIColor blueColor]];
        
        for (UIView *view in self.subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                UITextField *filed = (UITextField *)view;
                if (filed.tag == btn.tag) {
                    filed.userInteractionEnabled = YES;
                }
            }
        }
        
        
       
     
        
    } else {
        [btn setBackgroundColor:[UIColor blackColor]];
        for (UIView *view in self.subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                UITextField *filed = (UITextField *)view;
                if (filed.tag == btn.tag) {
                    filed.userInteractionEnabled = NO;
                    filed.text = @"";
                }
            }
        }
    
 }
}

- (void)btnClick:(UIButton*)btn {
    
    
    btn.selected = !btn.selected;
    if (btn.selected) {
        
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        // 设置颜色空间为rgb，用于生成ColorRef
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
        CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 255, 0, 0, 1 });
        // 设置边框颜色
        btn.layer.borderColor = borderColorRef;
        
        Index = btn.tag;
        
        //
        
    } else {
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 设置颜色空间为rgb，用于生成ColorRef
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        // 新建一个红色的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
        CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0, 250, 152, 1 });
        // 设置边框颜色
        btn.layer.borderColor = borderColorRef;
        
    }
    
    NSMutableArray *viewsArr = [NSMutableArray array];
        
        viewsArr = [NSMutableArray arrayWithArray:self.subviews];
    
    for (UIView *vie in viewsArr) {
        
        if ([vie isKindOfClass:[UIButton class]]) {
            
            UIButton *b = (UIButton *)vie;
            
            
            if ([b isEqual:btn]) {
                
                NSLog(@"1");
                
            }
            
        }
        
    }
    
}
@end
